import requests, json
import base64


#POST
def post(url, data):
    jpost = json.dumps(data)
    resp = requests.post(url, data=jpost)
    return resp

#GET
def get(url, data=None):
    jget = json.dumps(data)
    resp = requests.get(url, data=jget)
    return resp


if __name__ == '__main__':

    import cv2

    image = cv2.imread('test.jpg')

    #test get www.baidu.com
    try:
        resp = get('http://www.baidu.com')
        print(resp)
    except:
        print('GET FAILURE!')

    #test post base64
    data = {}
    data['keys'] = 'sys_temp'
    data['base64'] = base64.b64encode(cv2.imencode('.jpg', image)[1]).decode()
    try:
        resp = post('http://127.0.0.1:10088/post', data)
        print(resp)
    except:
        print('POST FAILURE!')

    #opencv imshow
    cv2.imshow('demo', image)
    kval = cv2.waitKey(0)

    cv2.destroyAllWindows()

